package org.dromara.wms.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.dromara.common.web.core.BaseService;
import org.dromara.wms.domain.WmsProductOss;
import org.dromara.wms.mapper.WmsProductOssMapper;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 产品管理Service业务层处理
 *
 * @author wenrui
 *
 */
@RequiredArgsConstructor
@Service
public class WmsProductOssServiceImpl extends BaseService {

  private final WmsProductOssMapper baseMapper;

  /**
   * 查询产品&图片
   */
  public List<WmsProductOss> queryById(Long id) {
    QueryWrapper<WmsProductOss> lqw = buildQueryWrapper(id);
    return baseMapper.selectList(lqw);
  }
  private QueryWrapper<WmsProductOss> buildQueryWrapper(Long id) {
    QueryWrapper<WmsProductOss> lqw = new QueryWrapper<>();
    lqw.eq(id != null,"product_id", id);
    return lqw;
  }

  /**
   * 新增产品&图片
   */
  public Boolean insertByBo(WmsProductOss bo) {
      return baseMapper.insert(bo) > 0;
  }

  /**
   * 列表新增产品&图片
   */
  public void insertByArray(List<WmsProductOss> bo) {
    baseMapper.deleteById(bo.get(0).getProductId());
    baseMapper.addOssBatch(bo);
  }

  /**
   * 修改产品&图片
   */
  public Boolean updateByBo(WmsProductOss bo) {
    return baseMapper.updateById(bo) > 0;
  }

  /**
   * 删除产品&图片
   */
  public void deleteByPicId(Long id) {
    Map<String, Object> ossIdmap = new HashMap<>();
    ossIdmap.put("oss_id", id);
    baseMapper.deleteByMap(ossIdmap);
  }

  /**
   * 删除产品&图片
   */
  public void deleteByProductId(Long id) {
    baseMapper.deleteById(id);
  }

  /**
   * 查询全部对应产品&图片的数量
   */
  public Long countAll(Long id) {
    QueryWrapper<WmsProductOss> lqw = buildQueryWrapper(id);
    return baseMapper.selectCount(lqw);
  }

  /**
   * 删除
   */
  public Boolean deleteByBo(WmsProductOss bo) {
    QueryWrapper<WmsProductOss> lqw = buildDeleteWrapper(bo);
    return baseMapper.delete(lqw) > 0;
  }
  private QueryWrapper<WmsProductOss> buildDeleteWrapper(WmsProductOss bo) {
    QueryWrapper<WmsProductOss> lqw = new QueryWrapper<>();
    lqw.eq(bo.getProductId() != null,"product_id", bo.getProductId());
    lqw.eq(bo.getOssId() != null,"oss_id", bo.getOssId());
    return lqw;
  }

}
